//
//  PinYinItemView.m
//  ToolLibrary
//
//  Created by 蓓蕾 on 2024/1/22.
//  Copyright © 2024 Yang. All rights reserved.
//

#import "PinYinItemView.h"
#import "NSString+YY.h"
#import "NSString+Regex.h"
#import "Masonry.h"

@interface PinYinItemView ()

@property (nonatomic, strong) UILabel *pinYinView;
@property (nonatomic, strong) UILabel *wordView;

@end

@implementation PinYinConfig


@end

@implementation PinYinItemView

-(id)initWithConfig:(PinYinConfig *)config {
    _config = config;
    return [self initWithFrame:CGRectZero];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
//    self.backgroundColor = [UIColor orangeColor];
    if (self) {
        UIView *bgView = [[UIView alloc] init];
        [self addSubview:bgView];
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.mas_centerX).mas_offset(0);
            make.centerY.mas_equalTo(self.mas_centerY).mas_offset(0);
            make.right.mas_lessThanOrEqualTo(self.mas_right).mas_offset(0);
            make.bottom.mas_lessThanOrEqualTo(self.mas_bottom).mas_offset(0);
        }];
        
        UIFont *pinYinFont = [UIFont systemFontOfSize:12];
        UIFont *wordFont = [UIFont systemFontOfSize:12];
        UIColor *pinYinTextColor = [UIColor blackColor];
        UIColor *wordTextColor = [UIColor blackColor];
        CGFloat margin = 2;
        NSValue *viewSizeValue;
        CGFloat itemWidth = 0;
        CGFloat itemHeight = 0;
        if (_config) {
            pinYinFont = _config.pinYinFont ? _config.pinYinFont : (_config.font ? _config.font : [UIFont systemFontOfSize:12]);
            wordFont = _config.wordFont ? _config.wordFont : (_config.font ? _config.font : [UIFont systemFontOfSize:12]);
            margin = [NSString isBlankString:_config.margin] ? 2 : [_config.margin floatValue];
            pinYinTextColor = _config.pinYinColor ? _config.pinYinColor : (_config.color ? _config.color : [UIColor blackColor]);
            wordTextColor = _config.wordColor ? _config.wordColor : (_config.color ? _config.color : [UIColor blackColor]);
            itemWidth = _config.itemWidth > 0 ? _config.itemWidth : 0;
            itemHeight = _config.itemHeight > 0 ? _config.itemHeight : 0;
            viewSizeValue = _config.viewSize ? _config.viewSize : nil;
        }
        
        _pinYinView = [[UILabel alloc] init];
        [bgView addSubview:_pinYinView];
        _pinYinView.text = @"拼音";
        CGFloat pinYinHeight = [@"zhǎojiāo" yy_maxHeightWithFont:pinYinFont Width:MAXFLOAT];
        CGFloat wordHeight = [@"矗立龘龘" yy_maxHeightWithFont:wordFont Width:MAXFLOAT];
        _pinYinView.font = pinYinFont;
        _pinYinView.textColor = pinYinTextColor;
        [_pinYinView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(bgView.mas_top).mas_offset(0);
            make.centerX.mas_equalTo(bgView.mas_centerX).mas_offset(0);
            make.right.mas_lessThanOrEqualTo(bgView.mas_right).mas_offset(0);
            make.bottom.mas_lessThanOrEqualTo(bgView.mas_bottom).mas_offset(0);
            make.height.mas_greaterThanOrEqualTo(pinYinHeight);
//            make.height.mas_lessThanOrEqualTo(pinYinHeight);
        }];
        
        _wordView = [[UILabel alloc] init];
        [bgView addSubview:_wordView];
        _wordView.text = @"汉字";
        _wordView.font = wordFont;
        _wordView.textColor = wordTextColor;
        [_wordView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_pinYinView.mas_bottom).mas_offset(margin);
            make.centerX.mas_equalTo(bgView.mas_centerX).mas_offset(0);
            make.right.mas_lessThanOrEqualTo(bgView.mas_right).mas_offset(0);
            make.bottom.mas_lessThanOrEqualTo(bgView.mas_bottom).mas_offset(0);
            make.height.mas_greaterThanOrEqualTo(wordHeight);
//            make.height.mas_lessThanOrEqualTo(wordHeight);
        }];

        if (viewSizeValue) {
            CGSize size = [viewSizeValue CGSizeValue];
            [self mas_updateConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(size);
            }];
        } else {
            if (itemWidth > 0) {
                [self mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.width.mas_equalTo(itemWidth);
                }];
            }
            if (itemHeight > 0){
                [self mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(itemHeight);
                }];
            }
        }
    }
    return self;
}

- (void)setPinYinString:(NSString *)pinYinString {
    _pinYinString = pinYinString;
    if ([pinYinString checkIsPunctuationMark]) {
        _pinYinView.hidden = YES;
    } else {
        _pinYinView.hidden = NO;
    }
    _pinYinView.text = pinYinString;
}

- (void)setWordString:(NSString *)wordString {
    _wordString = wordString;
    _wordView.text = wordString;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
